#include "departmentpage.h"
#include <QVBoxLayout>
#include <QHeaderView>
#include <QTableWidgetItem>
#include <QMessageBox>

DepartmentPage::DepartmentPage(QWidget *parent) : QWidget(parent) {
    tableWidget = new QTableWidget(this);

    // 主布局
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(tableWidget);
    setLayout(layout);

    connect(tableWidget, &QTableWidget::cellClicked, this, &DepartmentPage::onDepartmentClicked); // 连接信号
}

void DepartmentPage::setupTable(const QList<QJsonObject> &departmentsData) {
    // 清空之前的数据
    tableWidget->setRowCount(0);

    // 设置列数
    tableWidget->setColumnCount(2);
    QStringList headers = {"部门名称", "部门描述"};
    tableWidget->setHorizontalHeaderLabels(headers);

    // 设置垂直和水平头部隐藏
    tableWidget->verticalHeader()->setVisible(false); // 隐藏行号
    tableWidget->horizontalHeader()->setVisible(true); // 保留表头

    // 设置表格背景、边框等样式
    tableWidget->setStyleSheet(
        "QTableWidget {"
        "    border: 1px solid #ccc;"
        "    background-color: #f4f4f4;"
        "    color: #333;"
        "    font-family: 'Arial', sans-serif;"
        "    font-size: 16px;"
        "    border-radius: 6px;"
        "    outline: none;"
        "    gridline-color: transparent;"  // 移除表格交叉线
        "}"
        "QHeaderView::section {"
        "    background-color: #2d2d2d;"
        "    color: white;"
        "    border: none;"
        "    padding: 8px;"
        "    font-weight: normal;"
        "    font-size: 14px;"
        "}"
        "QTableWidget::item {"
        "    border-bottom: 1px solid #e0e0e0;"
        "    padding: 10px;"
        "    background-color: #ffffff;"
        "    border-radius: 4px;"
        "}"
        "QTableWidget::item:hover {"
        "    background-color: #e8e8e8;"
        "}"
        "QTableWidget::item:selected {"
        "    background-color: #c2c2c2;"
        "}"
    );

    // 添加部门信息
    for (const QJsonObject &department : departmentsData) {
        int row = tableWidget->rowCount();
        tableWidget->insertRow(row);

        // 设置部门名称和描述
        tableWidget->setItem(row, 0, new QTableWidgetItem(department["DepartmentName"].toString()));
        tableWidget->setItem(row, 1, new QTableWidgetItem(department["Description"].toString()));

        // 设置单元格为只读
        for (int col = 0; col < 2; ++col) {
            QTableWidgetItem *item = tableWidget->item(row, col);
            item->setFlags(item->flags() & ~Qt::ItemIsEditable);
        }
    }

    // 设置表格头部自适应列宽
    tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
}

void DepartmentPage::loadData()
{
    tcp = new ClientTcp(DEPARTMENTS_DATA);

    QString sql = "select * from Departments";
    tcp->sendMsg(sql);

    connect(tcp->socket, &QTcpSocket::readyRead, [=]() {
        setupTable(tcp->getDepartments());
    });
}

void DepartmentPage::onDepartmentClicked(int row, int column) {
    if (column == 0) { // 仅当点击部门名称列时触发
        QString departmentName = tableWidget->item(row, column)->text();
        emit departmentSelected(departmentName); // 发出信号
    }
}

DepartmentPage::~DepartmentPage() {
    if (tcp) {
        delete tcp; // 释放 ClientTcp 对象
        tcp = nullptr;
    }
    if (dp) {
        delete dp; // 释放 DoctorsInDepartmentPage 对象
        dp = nullptr;
    }
}
